package com.java.web;

import com.java.dao.commDao;
import com.java.dao.commDaoImpl;
import com.java.dao.userDao;
import com.java.dao.userDaoImpl;
import com.java.doMain.commodity;
import com.java.doMain.user;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author xyd
 * @Date 2021/12/23 18:58
 * @Version 1.0
 */
public class otherRequestServlet extends HttpServlet {

    private com.java.dao.commDao commDao = new commDaoImpl();
    private com.java.dao.userDao userDao = new userDaoImpl();

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        user user = (com.java.doMain.user) request.getSession().getAttribute("user");

        if (user!=null){
            user user1 = userDao.selectByUsername(user.getUsername());
            request.getSession().setAttribute("temp",user1);
            String[] str = new String[0];
            try {
                str = user1.getB2().split(",");
            } catch (Exception e) {
            }
            List<commodity> list = new ArrayList<>();
            for (int i=1;i<str.length;i++){
                try {
                    if (str[i]!=null||!str[i].equals("")){
                        list.add(commDao.selectByCid(Integer.parseInt(str[i])));
                    }
                }catch (Exception e){}
            }

            request.setAttribute("comm",list);
            commodity[] objects = list.toArray(new commodity[0]);

            double money = 0;
            for (int j = 0;j<objects.length;j++){
                try{
                    money+=objects[j].getcPrice();
                }catch (Exception e){

                }
            }
            request.setAttribute("money",money);
            request.getRequestDispatcher(request.getContextPath() + "/client/cart.jsp").forward(request,response);
        }else {
            request.setAttribute("err","<h4><font color='red'>请先登录!</font></h4>");
            request.getRequestDispatcher(request.getContextPath() + "/client/login.jsp").forward(request,response);
        }
    }
}
